Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Chat SPBoucher AI8 * File : src/app/api/auth/passkeys/[id]/route.ts9 * Module : app10 * Repository : https://git.spboucher.ai/chat-spboucher-ai11 * Project Website : https://chat.spboucher.ai12 * Created : 2026-11-1713 * Last Modified : 2026-11-1714 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { z } from "zod";44import { guarded, json, body } from "@/lib/api";45import { sql } from "@/lib/db";46import { recoveryStatus } from "@/lib/auth/grants";47import { revokeAllSessions } from "@/lib/auth/session";48import { requestMeta } from "@/lib/auth/current";49import { audit } from "@/lib/auth/audit";5051export const PATCH = guarded<{ id: string }>(async (req, { params, session }) => {52 const b = await body(req, z.object({ label: z.string().trim().min(1).max(60) }));53 const r = await sql`UPDATE webauthn_credentials SET label = ${b.label} WHERE id = ${params.id} RETURNING id`;54 if (!r.length) return json({ error: "Introuvable" }, { status: 404 });55 await audit("passkey.rename", { ...(await requestMeta()), sessionPublicId: session.public_id });56 return json({ ok: true });57}, { recentAuth: true });5859/**60 * Revokes a passkey and closes the sessions opened with it. Refused when it would leave no way back in:61 * the last passkey can never be removed, and removing the second-to-last requires unused recovery codes.62 */63export const DELETE = guarded<{ id: string }>(async (_req, { params, session }) => {64 const n = (await sql<{ n: number }[]>`SELECT count(*)::int AS n FROM webauthn_credentials`)[0].n;65 if (n <= 1) return json({ error: "Impossible de révoquer la dernière clé d'accès : enregistres-en une autre d'abord.", code: "last_passkey" }, { status: 409 });66 if (n === 2 && (await recoveryStatus()).remaining === 0) return json({ error: "Génère d'abord des codes de récupération : il ne resterait qu'une seule clé d'accès et aucun chemin de secours.", code: "no_recovery_path" }, { status: 409 });67 // sessions first: deleting the credential nulls sessions.credential_id (ON DELETE SET NULL), after which they could no longer be matched68 const exists = await sql`SELECT 1 FROM webauthn_credentials WHERE id = ${params.id}`;69 if (!exists.length) return json({ error: "Introuvable" }, { status: 404 });70 const closed = await revokeAllSessions({ credentialId: params.id, reason: "passkey-revoked" });71 const r = await sql<{ label: string | null }[]>`DELETE FROM webauthn_credentials WHERE id = ${params.id} RETURNING label`;72 await audit("passkey.revoke", { ...(await requestMeta()), sessionPublicId: session.public_id }, { passkey: r[0]?.label ?? "", sessionsClosed: closed });73 return json({ ok: true, sessionsClosed: closed, signedOut: session.credential_id === params.id });74}, { recentAuth: true });75